package com.itheima.order.controller;

import com.itheima.order.pojo.Order;
import com.itheima.order.service.OrderService;
import com.itheima.response.UserOrderDTO;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private Mapper dozerMapper;
    @GetMapping
    public List<Order> findAll(){
        return orderService.findAll();
    }

    @GetMapping("/username")
    public List<UserOrderDTO> findOrderByName(@PathVariable("username")String username){
        List<UserOrderDTO> result = new ArrayList<>();
       List<Order> orderList =  orderService.findOrderByName(username);
        for (Order order : orderList) {
            UserOrderDTO map = dozerMapper.map(order, UserOrderDTO.class);
            result.add(map);
        }
        return result;
    }
}
